// 滚动插件-组件
<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: "Scroll",
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      scroll: null
    };
  },
  // 生命周期函数
  mounted () {
    // 创建BScroll 对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
      click: true
    });
    // 监听滚动位置
    if (this.probeType === 2 || this.probeType === 3) {
      this.scroll.on("scroll", position => {
        this.$emit("scroll", position);
      });
    }
    // 监听下拉到底部
    if (this.pullUpLoad) {
      this.scroll.on("pullingUp", () => {
        // console.log("上传加载更多");
        this.$emit("pullingUp");
      });
    }
  },
  // 监听事件方法
  methods: {
    // 回到给定参数的位置
    scrollTo (x, y, time = 300) {
      this.scroll && this.scroll.scrollTo(x, y, time);
    },
    // 加载更多
    finishPullUp () {
      this.scroll && this.scroll.finishPullUp();
    },
    // 重新读取滚动高度
    refresh () {
      this.scroll && this.scroll.refresh();
    },
    // 获取滚得的y值
    getScrollY () {
      return this.scroll ? this.scroll.y : 0
    }
  }
};
</script>

<style scoped></style>
